package com.csg.web.validation;

import com.csg.web.service.CustomDemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义校验器
 *
 * @author george
 */
@Slf4j
public class CustomConstraintValidator implements ConstraintValidator<CustomConstraint, Object> {

    @Autowired
    private CustomDemoService customDemoService;

    /**
     * 校验器初始化
     *
     * @param constraintAnnotation
     */
    @Override
    public void initialize(CustomConstraint constraintAnnotation) {
        log.info("CustomConstraintValidator 校验器初始化完成。");
    }

    /**
     * 校验逻辑实现
     *
     * @param value   需要校验的值
     * @param context 校验的上下文相关信息
     * @return
     */
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        customDemoService.validate(value.toString());
        log.info("校验对象的值为: " + value);
        return false;
    }
}
